//
//  DisclosureIndicatorView.m
//  FadeSDWebImage
//
//  Created by 冷悦 on 16/2/24.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "DisclosureIndicatorView.h"
@interface DisclosureIndicatorView()

@property (nonatomic, strong)CAShapeLayer *line;

@property (nonatomic, assign)CGFloat radius;

@property (nonatomic, assign)CGFloat lineWidth;

@end

@implementation DisclosureIndicatorView

#pragma mark - Override
- (void)drawRect:(CGRect)rect {
    if (self.disclosureIndicatorEnable == YES) {
        CGPoint refPoint = [self calcRefPoint];
        [self calculateDefaultLineWidthAndRadius:rect];
        [self createDisclosureIndicatorLineAtPoint:refPoint];
    } else {
        [self.line removeFromSuperlayer];
    }
}

#pragma mark - Private
/**
 *  计算相对的点
 *
 *  @return 相对点
 */
- (CGPoint)calcRefPoint{
    CGRect bounds = self.bounds;
    CGFloat offset = self.disclosureApexOffset + self.disclosureIndicatorWidth / 2.0;
    CGPoint refPoint;
    
    switch (self.direction) {
        case Right:{
            CGFloat x = CGRectGetMaxX(bounds)/3*2 - offset;
            CGFloat y = CGRectGetMidY(bounds) + self.disclosureSideOffset;
            refPoint = CGPointMake(x, y);
            break;
        }
        case Left:{
            CGFloat y = CGRectGetMidY(bounds) + self.disclosureSideOffset;
            refPoint = CGPointMake(offset, y);
            break;
        }
        case Top:{
            CGFloat x = CGRectGetMidX(bounds) + self.disclosureSideOffset;
            CGFloat y = CGRectGetMidY(bounds)/3*2;
            refPoint = CGPointMake(x, y);
            break;
        }
        case Bottom:{
            CGFloat x = CGRectGetMidX(bounds) + self.disclosureSideOffset;
            CGFloat y = CGRectGetMaxY(bounds)/3*2 - offset;
            refPoint = CGPointMake(x, y);
            break;
        }
            
        default:{
            CGFloat x = CGRectGetMaxX(bounds) - offset;
            CGFloat y = CGRectGetMinY(bounds) + self.disclosureSideOffset;
            refPoint = CGPointMake(x, y);
            break;
        }
    }
    
    return refPoint;
}

/**
 *  计算line的起始与结束的角度
 *
 *  @return 起始结束角度数组
 */
- (NSArray *)calRefAngles{
    CGFloat angle1;
    CGFloat angle2;
    
    switch (self.direction) {
        case Right:{
            angle1 = 135;
            angle2 = 225;
            break;
        }
        case Left:{
            angle1 = 45;
            angle2 = 315;
            break;
        }
        case Top:{
            angle1 = 45;
            angle2 = 135;
            break;
        }
        case Bottom:{
            angle1 = 225;
            angle2 = 315;
            break;
        }
        default:{
            angle1 = 135;
            angle2 = 225;
            break;
        }
    }
    
    NSArray *angles = @[@(angle1), @(angle2)];
    return angles;
}

/**
 *  计算以一点为圆心，给定角度与半径，在圆上的位置
 *
 *  @param center 圆心
 *  @param angle  给定的角度，使用0-360，而不是3.1415926方式
 *  @param radius 半径
 *
 *  @return 圆上的点
 */
- (CGPoint) calcCircleCoordinateWithCenter:(CGPoint)center
                                 withAngle:(CGFloat)angle
                                 andRadius:(CGFloat)radius{
    CGFloat x = radius * cosf(angle * M_PI / 180);
    CGFloat y = radius * sinf(angle * M_PI / 180);
    
    return CGPointMake(center.x + x, center.y + y);
}

/**
 *  计算线段，并重绘
 *
 *  @param refPoint 参照点
 */
- (void)createDisclosureIndicatorLineAtPoint:(CGPoint)refPoint{
    CGFloat radius = [self getDisclosureIndicatorLength];
    NSArray *angles = [self calRefAngles];
    CGFloat angle1 = [[angles firstObject] floatValue];
    CGFloat angle2 = [[angles lastObject] floatValue];
    
    CGPoint topPoint = [self calcCircleCoordinateWithCenter:refPoint
                                                  withAngle:angle1
                                                  andRadius:radius];
    
    CGPoint bottomPoint = [self calcCircleCoordinateWithCenter:refPoint
                                                     withAngle:angle2
                                                     andRadius:radius];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:topPoint];
    [path addLineToPoint:refPoint];
    [path addLineToPoint:bottomPoint];
    self.line.path = path.CGPath;
    self.line.lineWidth = [self getDisclosureIndicatorWidth];
    self.line.strokeColor = [self getDisclosureIndicatorColor].CGColor;
    self.line.fillColor = [UIColor clearColor].CGColor;
    self.line.lineJoin = kCALineJoinRound;
    self.line.lineCap = kCALineJoinRound;
    [self.layer addSublayer:self.line];
}

- (void)calculateDefaultLineWidthAndRadius:(CGRect)rect{
    CGFloat height = CGRectGetHeight(rect);
    CGFloat width = CGRectGetWidth(rect);
    CGFloat maxLength = MIN(height, width);
    self.radius = maxLength * 0.5;
    self.lineWidth = 2;
}

#pragma mark - Setters
- (void)setDisclosureIndicatorEnable:(BOOL)disclosureIndicatorEnable{
    _disclosureIndicatorEnable = disclosureIndicatorEnable;
    [self setNeedsDisplay];
}

- (void)setDisclosureIndicatorColor:(UIColor *)disclosureIndicatorColor{
    _disclosureIndicatorColor = disclosureIndicatorColor;
    [self setNeedsDisplay];
}

- (void)setDisclosureIndicatorLength:(CGFloat)disclosureIndicatorLength{
    _disclosureIndicatorLength = disclosureIndicatorLength;
    [self setNeedsDisplay];
}

- (void)setDisclosureIndicatorWidth:(CGFloat)disclosureIndicatorWidth{
    _disclosureIndicatorWidth = disclosureIndicatorWidth;
    [self setNeedsDisplay];
}

- (void)setDisclosureApexOffset:(CGFloat)disclosureApexOffset{
    _disclosureApexOffset = disclosureApexOffset;
    [self setNeedsDisplay];
}

- (void)setDisclosureSideOffset:(CGFloat)disclosureSideOffset{
    _disclosureSideOffset = disclosureSideOffset;
    [self setNeedsDisplay];
}

- (void)setDirection:(DisclosureIndicatorViewDir)direction{
    _direction = direction;
    [self setNeedsDisplay];
}

#pragma mark - Getters
- (CAShapeLayer *)line{
    if (_line == nil) {
        _line = [CAShapeLayer new];
    }
    
    return _line;
}

#pragma mark - Private Getters
- (CGFloat)getDisclosureIndicatorLength{
    return self.disclosureIndicatorLength <= 0 ? self.radius : self.disclosureIndicatorLength;
}

- (CGFloat)getDisclosureIndicatorWidth{
    return self.disclosureIndicatorWidth <= 0 ? self.lineWidth : self.disclosureIndicatorWidth;
}

- (UIColor *)getDisclosureIndicatorColor{
#ifndef kUIViewDisclosureIndicatorLineColor
#define kUIViewDisclosureIndicatorLineColor [UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1.0]
#endif
    return self.disclosureIndicatorColor == nil ? kUIViewDisclosureIndicatorLineColor : self.disclosureIndicatorColor;
}

@end
